package Servlet;

import DAO.ComplaintDAO;
import DAO.DAOFactory;
import DAO.MaintenanceDAO;
import DAO.OrderDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/Evaluation")

public class EvaluationServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String evaluation = req.getParameter("evaluation");
        int id = Integer.parseInt(req.getParameter("id"));
        String op = req.getParameter("op");
        PrintWriter out = resp.getWriter();
        resp.setContentType( "text/html; charset=UTF-8" );
        switch (op) {
            case "1"://Complaint
                ComplaintDAO complaintDAO = (ComplaintDAO) DAOFactory.getComplaintDAOInstance();
                try {
                    if(complaintDAO.evaluateComplaint(id, evaluation)){
                        out.print("<script>alert('评价成功!');window.location.href='MyComplaint'</script>");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "2"://Maintenance
                MaintenanceDAO maintenanceDAO = (MaintenanceDAO) DAOFactory.getMaintenanceDAOInstance();
                try {
                    maintenanceDAO.evaluateMaintenance(id, evaluation);
                    out.print("<script>alert('评价成功!');window.location.href='MyMaintenance'</script>");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "3"://Order
                OrderDAO orderDAO = (OrderDAO) DAOFactory.getOrderDAOInstance();
                try {
                    orderDAO.evaluateOrder(id, evaluation);
                    out.print("<script>alert('评价成功!');window.location.href='MyOrder.jsp'</script>");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
